using System.Linq;
using NHibernate;
using Wikey.Domain.Components;
using Wikey.Entity;

namespace Wikey.Domain.Repository
{
    public interface IDocumentRepository
    {
        int Save(Document document);
        Document Find(int id);
        Document FindByFileName(string id);
    }

    public class DocumentRepository : Repository, IDocumentRepository {
        public DocumentRepository(ISessionSource source) : base(source)
        {
        }

        public DocumentRepository(ISession session) : base(session)
        {
        }

        public int Save(Document document)
        {
            base.Save(document);

            return document.Id; 
        }

        public Document Find(int id)
        {
            var document = from d in Context().Documents
            where d.Id == id
            select d;

            return document.First();
        }

        public Document FindByFileName(string id)
        {
            var document = from d in Context().Documents
                           where d.Filename.Equals(id)
                           select d;
            return document.First();
        }
    }
}